/*
 * Ext JS Library 2.0 Alpha 1
 * Copyright(c) 2006-2007, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

function generateRiskGrid(title, region, url_form, url, name_xml) {
	var addRiskItem = function() {
		//var url_form = 'Risk Name/impacts';
		var name = new Ext.form.TextField({
	        fieldLabel: 'Nombre',
	        allowBlank: false,
			blankText: 'El campo es requerido',
	        name: 'name',
	        anchor: '90%' 
	    });
    
		var description = new Ext.form.TextArea({
	        fieldLabel: 'Descripcion',
	        allowBlank: false,
			blankText: 'El campo es requerido',
	        name: 'description',
	        anchor: '90%'  
	    }); 
		
		var formPanel = new Ext.form.FormPanel({  
			id: 'add-project-panel',
	        frame: true,
	        title:'Agregar ' + title,
			collapsible: true,
			region: 'east',
	        labelAlign: 'right',
	        labelWidth: 85,
	        width:300,
			height: 300,
	        waitMsgTarget: true,
			url: url_form,
	        items: [ name, description ]
	    });
	    var win = new Ext.Window({
	        width: 500,
	        height:400,
	        minWidth: 300,
	        minHeight: 250,
	        layout: 'fit',
	        plain:true,
			modal: true,
	        bodyStyle:'padding:5px;',
	        buttonAlign:'center',
	        items: formPanel,
			buttons: [{
				text:'Agregar',
				handler: function() {
					//TODO: Arreglar el XML para que sea generico
					var xmlDocument = '<?xml version="1.0" encoding="UTF-8" ?>';
					xmlDocument += '<' + name_xml + '>';
					xmlDocument += '<name>' + name.getValue() + '</name>';
					xmlDocument += '<description>' + description.getValue() + '</description>';
					xmlDocument += '</' + name_xml + '>';
					$.ajax({
					  type: "post",
					  url: url_form,
					  dataType: "xml",
					  data:{xml: xmlDocument},
					  success: function(xml){
									Ext.MessageBox.alert('Exito', 'El Item fue agregado');
									ds.reload();
									win.close();
								}
					});				
				
				}
			},{
				text: 'Cerrar',
				handler: function(){
					win.close();
				}
			}]
	    });

	    win.show();			
	};
	
	var deleteRiskItem = function() {
		var m = grid.getSelections();
		if(m.length > 0) {
			Ext.MessageBox.confirm('Mensaje', 'Esta seguro que desea eliminar el item?' , deleteProject2);	
		} else {
			Ext.MessageBox.alert('Mensaje', 'Por favor, selecciones al menos un item para eliminar.');
		}
	};

	//TODO: Arreglar la url para que sea generico
	var deleteProject2 = function(btn) {
		if(btn == 'yes') {
			var m = grid.getSelections();
			$.ajax({
				  type: "delete",
				  url: url + '/' +m[0].id,
				  success: function(){
								ds.remove(m[0]);
								Ext.MessageBox.alert('Exito', 'El item fue eliminado.');
							}
				});
		
			
		}
	};
	var reader = new Ext.data.XmlReader({
		   record: name_xml,
		   id: 'id'
	   }, [
		   'name',
		   'description'
	   ]);
    // create the Data Store
	var ds = new Ext.data.GroupingStore({
				url: url_form,
				reader: reader,
				sortInfo:{field: 'name', direction: "ASC"}          
		    });

    var cm =new Ext.grid.ColumnModel([
			{header: "Nombre", width: 200, dataIndex: 'name', editor: new Ext.form.TextField({allowBlank: false})},
			{header: "Descripcion", width: 500, dataIndex: 'description', editor: new Ext.form.TextField({allowBlank: false})}
	]);
	var sm = new Ext.grid.RowSelectionModel({singleSelect:true});
	var grid = new Ext.grid.EditorGridPanel({
		ds: ds,
        cm: cm,
		selModel: sm,
		//renderTo: 'grid',
		region: region,
        //width: 300,
        height:200,
		layout: 'fit',
		frame:true,
		collapsible: true,
		title: title,
		iconCls: 'icon-grid',
		tbar:[{
            text:'Agregar',
            tooltip:'Agregar item al riesgo',
            iconCls:'add',
			handler: addRiskItem
	        },
			{
            text:'Eliminar',
            tooltip:'Eliminar el item seleccionados',
            iconCls:'remove',
			handler: deleteRiskItem
	        }		
		]
    });
	ds.load();
	return grid;
}

